$(function() {
    var start = 0;
    var end = 0;
    var binfile = '';

    function GetAddressIP() {
        $.ajax({
            type: "GET",
            url: "lib/showid.php",
            success: function(msg) {
                $('#ip').html('<b>' + msg + '</b>');
            }
        });
    };

    function TestDownload() {
        start = new Date().getTime();
        $('#dlspeed').html('<img src="images/busy.gif">');
        $.ajax({
            type: "GET",
            url: "bin/file.bin",
            // dataType: 'application/octet-stream',
            success: function(msg) {
                binfile = msg;
                end = new Date().getTime();
                diff = (end - start) / 1000;
                bytes = msg.length;
                speed = (bytes / diff) / 1024 / 1024 * 8;
                speed = Math.round(speed*100)/100;
                $('#dlspeed').html('<b>' + speed + ' Mb/s</b>');
            },
            complete: function(xhr, textStatus) {
                TestUpload();
            }
        });
    };

    function TestUpload() {
        start = new Date().getTime();
        $('#ulspeed').html('<img src="images/busy.gif">');
        $.ajax({
            type: "POST",
            url: "bin/post.php",
            data: binfile,
            // dataType: 'application/octet-stream',
            success: function(msg) {
                end = new Date().getTime();
                diff = (end - start) / 1000;
                bytes = binfile.length;
                speed = (bytes / diff) / 1024 / 1024 * 8;
                speed = Math.round(speed*100)/100;
                $('#ulspeed').html('<b>' + speed + ' Mb/s</b>');
                // $('#ulbar').css('width', Math.floor(speed * 60)+'px');
            }    
        });
    };
    GetAddressIP();
    $("#test").click(function() {
        TestDownload();
    });
});